<?php
include(APP_HOME."/modules/systemapi/controllers/browser.php");
if(!class_exists('MAIN_Controller_Action')) Zend_Loader :: loadClass('MAIN_Controller_ActionFront');
class Front_PaymentController extends MAIN_Controller_ActionFront {
	public function paymentAction() {
		$view = $this->_getView();
		$auth = Zend_Auth :: getInstance();
		$identity = $auth->getIdentity();
		$view->assign("today",date("d/m/Y"));
		$view->assign("now",getToday());
		$view->output('payment/payment.tpl');
    }
    public function loadtplAction(){
    	$params   = $this->getParams();
    	$view = $this->_getView();
    	$view->output('payment/'.$params['tpl'].'.tpl');
    }
    public function getpaymentlistAction(){
    	$params   = $this->getParams();
    	//_print($params);
    	$generic = $this->getGeneric();
    	$view = $this->_getView();
    	$data = $generic->getpaymentlist($params);
    	if($params['menu_num'] == '1'){
    		$link	=	'paymentpa';
    	} else if($params['menu_num'] == '2'){
    		$link 	=	'paymenter';
    	} else if($params['menu_num'] == '3'){
    		$link 	=	'paymentopd';
    	}
    	
    	$view->assign("data",$data);
    	$view->assign("link",$link);
    	$data['htmls'] = $view->outputhtml('ajax/_view_payment_list.tpl');
		$this->_helper->json($data);
    }
    public function paymentpaAction(){
    	if(!class_exists('Zend_Auth'))Zend_loader :: loadClass('Zend_Auth');
    	$params   = $this->getParams();
    	$generic = $this->getGeneric();
    	$view = $this->_getView();
    	$today = getToday();
    	$payment_datetime	=	date('Y-m-d H:i:s');
    	$auth = Zend_Auth :: getInstance();
    	$identity = $auth->getIdentity(); 	
    	$paymentId = '';//$generic->getPaymentId();
    	$preauthData = $generic->getPreAuthById($params['preauth_id'],$identity->userid);
    	$policyData = $generic->getPolicyById($preauthData['0']['policyid']);
    	$claimGetCover	=	$generic->getClaimCover();
    	$accList = $generic->getAccList($preauthData['0']['policyid'],'PA');   	
    	$deducList = $generic->getDeducList($preauthData['0']['policyid']);
		
    	if(empty($deducList)){
    		$deducList[0]['deducper'] = '-';
    		$deducList[0]['dgroup'] = $policyData[effective];
    		$deducList[0]['deduc'] = number_format($policyData[deductible],2,'.',',');
    	}
    	else{
    		foreach ($deducList as $key=>$value){
    			$deducList[$key]['deduc'] = number_format(($policyData[deductible] - $deducList[$key]['deduc']),2,'.',',');
    			
    		}
    	}
    	
    	$arr_hour = getHour();
    	$arr_min = getMin();
    	$default_date = date('d/m/Y');
    	$default_hour = date(' H');
    	$default_min = date(' i');
     	
    	if($policyData['deductible']	!= '0' and $policyData['deductible'] != ''){
    		$decuctible	=	number_format ( $policyData['deductible'], 2, ".", "," );
    	} else {
    		$decuctible	=	'';
    	}
    	
    	if($policyData['copayment']	!= '0' and $policyData['copayment'] != ''){
    		$copayment	=	number_format ( $policyData['copayment'], 2, ".", "," );
    	} else {
    		$copayment	=	'';
    	}
    	//_print($preauthData);
    	$policyData[deductible] = number_format($policyData[deductible],2,'.',',');
    	$img = 'title_pa.png';
    	$menuseq	=	'PA';
    	$view->assign("img",$img);
    	$view->assign("menuseq",$menuseq);
    	$view->assign("paymentId",$paymentId);
    	$view->assign("preauthData",$preauthData['0']);
    	$view->assign("today",$today);
    	$view->assign("payment_datetime",$payment_datetime);
    	$view->assign("policyData",$policyData);
    	$view->assign("claimGetCover",$claimGetCover);
    	//$view->assign("accList",$al);
		$view->assign("accList",$accList);
    	$view->assign("title",'Payment');
    	//$view->assign("default_date",date('Y-m-d'));
    	$view->assign("default_date",$default_date);
    	$view->assign("default_hour",$default_hour);
    	$view->assign("default_min",$default_min);
    	$view->assign("arr_hour",$arr_hour);
    	$view->assign("arr_min",$arr_min);
    	$view->assign("decuctible",$decuctible);
    	$view->assign("copayment",$copayment);
    	$view->assign("deducList",$deducList);
    	$view->output('payment/payment_pa.tpl');
    	//_print($claimGetCover);
    	//echo $identity->userid;
    }
    public function paymenterAction(){
    	
    	if(!class_exists('Zend_Auth'))Zend_loader :: loadClass('Zend_Auth');
    	$params   = $this->getParams();
    	$generic = $this->getGeneric();
    	$view = $this->_getView();
    	$today = getToday();
    	$payment_datetime	=	date('Y-m-d H:i:s');
    	$auth = Zend_Auth :: getInstance();
    	$identity = $auth->getIdentity();
    	
    	$paymentId = '';//$generic->getPaymentId(); //--beerz
    	$preauthData = $generic->getPreAuthById($params['preauth_id'],$identity->userid);
    	$policyData = $generic->getPolicyById($preauthData['0']['policyid']);
    	if(empty($policyData)) {
    		echo "<script type='text/javascript'>alert('ขออภัยไม่สามารถเลือกรายการนี้ได้ กรุณาติดต่อ HBC')</script>";
    		echo "<script langquage='javascript'>window.location='".APP_URL."/payment/'</script>";
    	}

    	$PlanData = $generic->getPlan2catByPlanId($policyData['plan_id'],$preauthData['0']['policyid']);
    	$PlanData['opd'] = number_format($PlanData['opd'],2,'.',',');
    	$PlanData['rb'] = number_format($PlanData['rb'],2,'.',',');
    	$PlanData['er'] = number_format($PlanData['er'],2,'.',',');
    	//_print($PlanData); exit();
    	$claimGetCover	=	$generic->getClaimCover();
    	$accList = $generic->getAccList($preauthData['0']['policyid'],'ER');
    	$deducList = $generic->getDeducList($preauthData['0']['policyid']);
		
    	if(empty($deducList)){
    		$deducList[0]['deducper'] = '-';
    		$deducList[0]['dgroup'] = $policyData[effective];
    		$deducList[0]['deduc'] = number_format($policyData[deductible],2,'.',',');
    	}
    	else{
    		foreach ($deducList as $key=>$value){
    			$deducList[$key]['deduc'] = number_format(($policyData[deductible] - $deducList[$key]['deduc']),2,'.',',');
    			
    		}
    	}
    	
    	$policyData[deductible] = number_format($policyData[deductible],2,'.',',');
    	$arr_hour = getHour();
    	$arr_min = getMin();
    	$default_date = date('d/m/Y');
    	$default_hour = date(' H');
    	$default_min = date(' i');
    	$day	=	explode(" ",$accList);
    	$dmy	= 	explode("-",$day[0]);
    	$d	=	$dmy[2];
    	$m	=	$dmy[1];
    	$y	=	$dmy[0]+543;
    	$al	= $d."/".$m."/".$y." ".$day[1];	
    	//_print($ymd); exit();
    	$img = 'title_health_er.png';
    	$menuseq	=	'HE';
    	$view->assign("img",$img);
    	$view->assign("menuseq",$menuseq);
    	$view->assign("paymentId",$paymentId);
    	$view->assign("preauthData",$preauthData['0']);
    	$view->assign("today",$today);
    	$view->assign("payment_datetime",$payment_datetime);
    	$view->assign("policyData",$policyData);
    	$view->assign("PlanData",$PlanData);
    	$view->assign("claimGetCover",$claimGetCover);
    	//$view->assign("accList",$al);
		$view->assign("accList",$accList);
    	$view->assign("default_date",$default_date);
    	$view->assign("default_hour",$default_hour);
    	$view->assign("default_min",$default_min);
    	$view->assign("arr_hour",$arr_hour);
    	$view->assign("arr_min",$arr_min);
    	$view->assign("deducList",$deducList);
    	$view->output('payment/payment_er.tpl');
    }
    public function paymentopdAction(){
    	if(!class_exists('Zend_Auth'))Zend_loader :: loadClass('Zend_Auth');
    	$params   = $this->getParams();
    	$generic = $this->getGeneric();
    	$view = $this->_getView();
    	$today = getToday();
    	$payment_datetime	=	date('Y-m-d H:i:s');
    	$auth = Zend_Auth :: getInstance();
    	$identity = $auth->getIdentity();
    	
    	$paymentId = '';//$generic->getPaymentId();
    	$preauthData = $generic->getPreAuthById($params['preauth_id'],$identity->userid);
    	$policyData = $generic->getPolicyById($preauthData['0']['policyid']);
    	$PlanData = $generic->getPlan2catByPlanId($policyData['plan_id'],$preauthData['0']['policyid']);
    	$PlanData['opd'] = number_format($PlanData['opd'],2,'.',',');
    	$PlanData['rb'] = number_format($PlanData['rb'],2,'.',',');
    	$PlanData['er'] = number_format($PlanData['er'],2,'.',',');
    	$claimGetCover	=	$generic->getClaimCover();
    	$LastAdmisOPD = $generic->getLastAdmisOPD($preauthData['0']['policyid']);
    	$accList = $generic->getAccList($preauthData['0']['policyid'],'OPD');
    	$deducList = $generic->getDeducList($preauthData['0']['policyid']);
		
    	if(empty($deducList)){
    		$deducList[0]['deducper'] = '-';
    		$deducList[0]['dgroup'] = $policyData[effective];
    		$deducList[0]['deduc'] = number_format($policyData[deductible],2,'.',',');
    	}
    	else{
    		foreach ($deducList as $key=>$value){
    			$deducList[$key]['deduc'] = number_format(($policyData[deductible] - $deducList[$key]['deduc']),2,'.',',');
    			
    		}
    	}
    	
    	$policyData[deductible] = number_format($policyData[deductible],2,'.',',');
    	
    	$arr_hour = getHour();
    	$arr_min = getMin();
    	$default_date = date('d/m/Y');
    	$default_hour = date(' H');
    	$default_min = date(' i');
    	$day	=	explode(" ",$accList);
    	$dmy	= 	explode("-",$day[0]);
    	$d	=	$dmy[2];
    	$m	=	$dmy[1];
    	$y	=	$dmy[0]+543;
    	$al	= $d."/".$m."/".$y." ".$day[1];
    	$img = 'title_health_opd.png';
    	$menuseq	=	'HO';
    	$view->assign("img",$img);
    	$view->assign("menuseq",$menuseq);
    	$view->assign("paymentId",$paymentId);
    	$view->assign("preauthData",$preauthData['0']);
    	$view->assign("today",$today);
    	$view->assign("payment_datetime",$payment_datetime);
    	$view->assign("policyData",$policyData);
    	$view->assign("PlanData",$PlanData);
    	$view->assign("claimGetCover",$claimGetCover);
    	//$view->assign("accList",$al);
		$view->assign("accList",$accList);
    	$view->assign("default_date",$default_date);
    	$view->assign("default_hour",$default_hour);
    	$view->assign("default_min",$default_min);
    	$view->assign("arr_hour",$arr_hour);
    	$view->assign("arr_min",$arr_min);
    	$view->assign("deducList",$deducList);
    	$view->assign("LastAdmisOPD",$LastAdmisOPD);
    	
    	$view->output('payment/payment_opd.tpl');
    }
    public function getpaymentconfirmAction(){
    	$params   = $this->getParams();
    	$view = $this->_getView();
    	
    	$acc_date = split('/',$params['acc_date']);
    	$data['acc_date'] = $acc_date['2'].'-'.$acc_date['1'].'-'.$acc_date['0'].' '.$params['acc_time'];
    	
    	$adm_date = split('/',$params['admis_date']);
    	$data['admis_date'] = $adm_date['2'].'-'.$adm_date['1'].'-'.$adm_date['0'].' '.$params['adm_time'];
   // echo $params['last_acc'];exit();
   		
    	if($params['last_acc'] != '0'){
	    	$la	=	split(" ",$params['last_acc']);
	    	$last_acc_date	=	split("/",$la['0']);
	    	$ld	=	$last_acc_date['0'];
	    	$lm	=	$last_acc_date['1'];
	    	$ly	=	$last_acc_date['2'];
	    	
	    	$data['last_acc']	=	$ly."-".$lm."-".$ld." ".$la['1'].":00.0000";
    	} else {
    		$data['last_acc']	=	'0';
    	}
    	
    	$data['payment_id'] = $params['payment_id'];
    	$data['userid'] = $params['userid'];
    	$data['preauth_id'] = $params['preauth_id'];
    	$data['policy_no'] = $params['policy_no'];
    	$data['payment_datetime'] = $params['payment_datetime'];
    	$data['payment_status'] = $params['payment_status'];
    	$data['customer_type'] = $params['customer_type'];
    	$data['policy_id'] = $params['policy_id'];
    	$data['payment_type'] = $params['payment_type'];
    	$data['deductible'] = $params['deductible'];
    	$data['charge'] = $params['charge'];
    	$data['discount'] = $params['discount'];
    	$data['co_payment'] = $params['co_payment'];
    	$data['pre_approved'] = $params['pre_approved'];
    	$data['non_covered'] = $params['non_covered'];
    	$data['indiation_admit'] = $params['indiation_admit'];
    	//$data['cover_type'] = $params['cover_type'];
    	//_print($data); exit();
    	$view->assign("data",$data);
    	$data['htmls'] = $view->outputhtml('ajax/_view_payment_confirm.tpl');
    	$this->_helper->json($data);
    	
    }
    
    function get_client_ip() {
    	$ipaddress = '';
    	if ($_SERVER['HTTP_CLIENT_IP'])
    		$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    	else if($_SERVER['HTTP_X_FORWARDED_FOR'])
    		$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    	else if($_SERVER['HTTP_X_FORWARDED'])
    		$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    	else if($_SERVER['HTTP_FORWARDED_FOR'])
    		$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    	else if($_SERVER['HTTP_FORWARDED'])
    		$ipaddress = $_SERVER['HTTP_FORWARDED'];
    	else if($_SERVER['REMOTE_ADDR'])
    		$ipaddress = $_SERVER['REMOTE_ADDR'];
    	else
    		$ipaddress = 'UNKNOWN';
    
    	return $ipaddress;
    }
    
    public function insertpaymentAction(){
    	$params = $this->getParams();
    	$generic = $this->getGeneric();
    	$paymentId = $generic->getPaymentId(); //--beerz
    	$browser = new Browser();
    	$get_browser = $browser->getBrowser();
    	$get_browser_version = $browser->getVersion();
    	$client_ip = file_get_contents('http://phihag.de/ip/');
    	$client_ip = $client_ip.'-'.$this->get_client_ip();
    	$generic->insertPayment($params,$paymentId,$get_browser.$get_browser_version,$client_ip);
    	$view = $this->_getView();
    	$data['htmls'] = $view->outputhtml('ajax/_view_insert_payment.tpl');    	
    	$this->_helper->json($data);
    }
    public function calpreapproveAction(){
    	$params = $this->getParams();
    	$generic = $this->getGeneric();
    	echo $generic->getClaimPreApp($params);
    }
    
}
?>